package com.mju.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mju.controller.common.BaseController;
import com.mju.controller.common.JsonResult;
import com.mju.pojo.VipPriceInfo;
import com.mju.service.IVipPriceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Slf4j
@RestController
@RequestMapping("/vipPrice")
@Api(tags = "VIP价格")
public class VipPriceController extends BaseController {
    @Autowired
    private IVipPriceService vipPriceService;

    @ApiOperation("添加会员价格信息")
    @PostMapping("/addVipPriceInfo")
    public JsonResult addVipPriceInfo(@RequestBody VipPriceInfo vipPriceInfo) {
        String message = vipPriceService.addVipPriceInfo(vipPriceInfo) ? "添加会员价格信息成功" : "添加会员价格信息失败";
        return new JsonResult(OK, message);
    }

    @ApiOperation("删除会员价格信息")
    @PutMapping("/deleteVipPriceInfo")
    public JsonResult deleteVipPriceInfo(@RequestParam("vPid") Integer vPid) {
        String message = vipPriceService.deleteVipPriceInfo(vPid) ? "删除会员价格信息成功" : "删除会员价格信息失败";
        return new JsonResult(OK, message);
    }

    @ApiOperation("修改会员价格信息")
    @PutMapping("/updateVipPriceInfo")
    public JsonResult updateVipPriceInfo(@RequestBody VipPriceInfo vipPriceInfo) {
        String message = vipPriceService.updateVipPriceInfo(vipPriceInfo) ? "修改会员价格信息成功" : "修改会员价格信息失败";
        return new JsonResult(OK, message);
    }

    @ApiOperation("修改会员价格")
    @PutMapping("/updateVipPriceByVPid")
    public JsonResult updateVipPriceByVPid(@RequestParam("vPid") Integer vPid, BigDecimal vipPrice) {
        String message = vipPriceService.updateVipPriceByVPid(vPid, vipPrice) ? "修改会员价格成功" : "修改会员价格失败";
        return new JsonResult(OK, message);
    }

    @ApiOperation("查询某会员价格")
    @GetMapping("/getVipPriceByVPid/{vPid}")
    public JsonResult getVipPriceByVPid(@PathVariable("vPid") Integer vPid) {
        BigDecimal vipPrice = vipPriceService.getVipPriceByVPid(vPid);
        return new JsonResult(OK, vipPrice);
    }

    @ApiOperation("查询某会员折扣(为-1.00则为查询不到)")
    @GetMapping("/getDiscountByVipName/{vipName}")
    public JsonResult getDiscountByVipName(@PathVariable("vipName") String vipName) {
        BigDecimal vipPrice = vipPriceService.getDiscountByVipName(vipName);
        return new JsonResult(OK, vipPrice);
    }

    @ApiOperation("无条件查询所有会员价格信息")
    @GetMapping("/{currentPage}/{pageSize}")
    public JsonResult<IPage<VipPriceInfo>> queryAllVipPriceInfo(
            @PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize) {
        IPage<VipPriceInfo> data = vipPriceService.queryAllVipPriceInfo(currentPage, pageSize);
        return new JsonResult<>(OK, data);
    }

    @ApiOperation("条件查询所有会员价格信息(条件：会员价格表id，会员名称)")
    @PostMapping("/conQueryAllVipPriceInfo")
    public JsonResult<IPage<VipPriceInfo>> conQueryAllVipPriceInfo(
            @RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pageSize, @RequestBody VipPriceInfo vipPriceInfo) {
        IPage<VipPriceInfo> data = vipPriceService.conQueryAllVipPriceInfo(currentPage, pageSize, vipPriceInfo);
        //如果当前页码值大于了总页码值，那么重新执行查询操作，使用最大页码值作为当前页码值
        if (currentPage > data.getPages()) {
            data = vipPriceService.conQueryAllVipPriceInfo((int) data.getPages(), pageSize, vipPriceInfo);
        }
        return new JsonResult<>(OK, data);
    }
}
